package com.aims.server.handler;

import com.aims.server.enums.OperationType;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedTypes;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * OperationType枚举类型处理器
 * 用于MyBatis与数据库之间的类型转换
 */
@MappedTypes(OperationType.class)
public class OperationTypeHandler extends BaseTypeHandler<OperationType> {

    @Override
    public void setNonNullParameter(PreparedStatement ps, int i, OperationType parameter, JdbcType jdbcType) throws SQLException {
        ps.setString(i, parameter.getValue());
    }

    @Override
    public OperationType getNullableResult(ResultSet rs, String columnName) throws SQLException {
        String value = rs.getString(columnName);
        return value == null ? null : OperationType.fromValue(value);
    }

    @Override
    public OperationType getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
        String value = rs.getString(columnIndex);
        return value == null ? null : OperationType.fromValue(value);
    }

    @Override
    public OperationType getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
        String value = cs.getString(columnIndex);
        return value == null ? null : OperationType.fromValue(value);
    }
}